﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using SMA.GENTEv1.Dados;

namespace SMA.GENTEv1
{
    public class FrotaCombustivel
    {
        #region Variaveis Membro
        private int _idFrotaCombustivel;
        private string _descricao;
        private GENTEEntities _context;
        #endregion

        #region Parametros
        public int IdFrotaCombustivel
        {
            get { return _idFrotaCombustivel; }
            set { _idFrotaCombustivel = value; }
        }
        
        public string Descricao
        {
            get { return _descricao; }
            set { _descricao = value; }
        }
        #endregion

        #region Construtores
        public FrotaCombustivel()
        {
            _context = new GENTEEntities();
        }

        public FrotaCombustivel(int _idcombustivel, bool _carregaDados)
        {
            _context = new GENTEEntities();
            if (!_carregaDados)
                this._idFrotaCombustivel = _idcombustivel;
            else
                Carregar(_idcombustivel);
        }
        #endregion

        #region Métodos Privados
        private static Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
        {
            if (null == valueSelector)
            {
                throw new
                    ArgumentNullException("valueSelector");
            }
            if (null == values) { throw new ArgumentNullException("values"); }

            ParameterExpression p = valueSelector.Parameters.Single();
            if (!values.Any())
            {
                return e => false;
            }

            var equals = values.Select(value =>
            (Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value,
            typeof(TValue))));
            var body = equals.Aggregate<Expression>((accumulate, equal) =>
            Expression.Or(accumulate, equal));
            return Expression.Lambda<Func<TElement, bool>>(body, p);
        }

        private void Carregar(int _idcombustivel)
        {
            try
            {
                var consulta = (from combustivel in _context.tab_frota_combustivel
                                where combustivel.id_frota_combustivel == _idcombustivel
                                select combustivel);

                var listConsulta = consulta.ToList();

                foreach (var list in listConsulta)
                {
                    this._idFrotaCombustivel = list.id_frota_combustivel;
                    this._descricao = list.descricao;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private bool VerificaCadastro()
        {
            try
            {
                bool verifica = false;

                if (this._descricao == string.Empty)
                    throw new Exception("É necessário informar a descrição do combustível!");

                verifica = true;
                return verifica;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Métodos Públicos

        #region Métodos de Seleção
        public List<FrotaCombustivel> SelectAll()
        {
            try
            {
                List<FrotaCombustivel> listCombustivel = new List<FrotaCombustivel>();

                var consulta = (from combustivel in _context.tab_frota_combustivel
                                select combustivel);

                var listConsulta = consulta.ToList();

                foreach (var list in listConsulta)
                {
                    FrotaCombustivel combustivel = new FrotaCombustivel();
                    combustivel.IdFrotaCombustivel = list.id_frota_combustivel;
                    combustivel.Descricao = list.descricao;

                    listCombustivel.Add(combustivel);
                }

                return listCombustivel;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<FrotaCombustivel> SelectPor(int[] _idcombustivel)
        {
            try
            {
                List<FrotaCombustivel> listCombustivel = new List<FrotaCombustivel>();

                var consulta = (from combustivel in _context.tab_frota_combustivel.Where((BuildContainsExpression<tab_frota_combustivel, int>(item => item.id_frota_combustivel, _idcombustivel)))
                                select combustivel);

                var listConsulta = consulta.ToList();

                foreach (var list in listConsulta)
                {
                    FrotaCombustivel combustivel = new FrotaCombustivel();
                    combustivel.IdFrotaCombustivel = list.id_frota_combustivel;
                    combustivel.Descricao = list.descricao;

                    listCombustivel.Add(combustivel);
                }

                return listCombustivel;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Métodos de Persistência
        public void Salvar()
        {
            try
            {
                if (VerificaCadastro())
                {
                    if (this._idFrotaCombustivel == 0)
                    {
                        tab_frota_combustivel tabCombustivel = new tab_frota_combustivel();
                        tabCombustivel.descricao = this._descricao;

                        _context.AddTotab_frota_combustivel(tabCombustivel);
                        _context.SaveChanges();
                    }
                    else
                    {
                        tab_frota_combustivel tabCombustivel = (from combustivel in _context.tab_frota_combustivel
                                                                where combustivel.id_frota_combustivel == this._idFrotaCombustivel
                                                                select combustivel).First();
                        tabCombustivel.descricao = this._descricao;

                        _context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #endregion
    }
}
